package org.glut.AppStoreProject.controller;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.apache.log4j.Logger;
import org.glut.AppStoreProject.entity.App;
import org.glut.AppStoreProject.service.AppServiceImpl;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * @author: gluglu2
 * @date: 2025/7/14 11:36
 */
@WebServlet(value = {"/app"})
public class AppServlet extends HttpServlet {
    /**
     * @param req
     * @param resp
     * @description: app业务处理
     * @author: gluglu2
     * @date: 2025/7/14 12:54
     */
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setContentType("text/html;charset=utf-8");
        PrintWriter writer = resp.getWriter();
        AppServiceImpl appService = new AppServiceImpl();
        Gson gson = null;
        List<App> respList = new ArrayList<>();

        System.out.println("1111111111111111111111111111111111111");
        System.out.println(req.getParameter("download"));

        if (req.getParameter("download") == null) {
            String id = req.getParameter("id");
            App app = new App();
            if (id != null && !id.isBlank()) {
                app.setId(Integer.valueOf(id));
            }
            App targetApp = appService.getAppById(app);
            if (targetApp != null) {
                respList.add(targetApp);
            }

            List<App> apps = appService.getAppsByType(targetApp);
            if (apps != null) {
                apps.remove(targetApp.getId() - apps.get(0).getId()); // 去重
                Collections.shuffle(apps); // 随机打乱顺序
                respList.addAll(apps);
            }

            gson = new GsonBuilder()
                    .setDateFormat("yyyy-MM-dd HH:mm:ss")
                    .setPrettyPrinting()
                    .create();
            String respJson = gson.toJson(respList);
            writer.write(respJson);
        } else {
            String id = req.getParameter("id");
            App app = new App();
            if (id != null && !id.isBlank()) {
                app.setId(Integer.valueOf(id));
            }
            App targetApp = appService.getAppById(app);
            if (targetApp != null) {
                gson = new GsonBuilder()
                        .setDateFormat("yyyy-MM-dd HH:mm:ss")
                        .setPrettyPrinting()
                        .create();
                String respJson = gson.toJson(targetApp);
                writer.write(respJson);
            }
        }

    }

    /**
     * @param req
     * @param resp
     * @description:
     * @author: gluglu2
     * @date: 2025/7/14 12:46
     */
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }

    /**
     * @param req
     * @return java.lang.String
     * @description: 转换json格式
     * @author: gluglu2
     * @date: 2025/7/14 12:55
     */
    private String jsonFormRequest(HttpServletRequest req) {
        String line = null;
        StringBuilder buff = new StringBuilder();
        try {
            BufferedReader br = new BufferedReader(new InputStreamReader(req.getInputStream(), StandardCharsets.UTF_8));
            while ((line = br.readLine()) != null) {
                buff.append(line);
            }
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        return buff.toString();
    }
}
